package com.zhouzhou.fastjava.util;

import com.google.common.net.MediaType;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;

import java.io.File;
import java.util.List;

public abstract class FileKit {

    private static final Tika tika = new Tika();

    public static String getConentType(String filename) {
        String contentType = tika.detect(filename);
        if (StringUtils.isBlank(contentType)) {
            contentType = MediaType.OCTET_STREAM.toString();
        }
        return contentType;
    }

    public static File tryGetFile(File folder, String filename, List<String> extensions) {
        for (String extension : extensions) {
            File file = new File(folder, filename + "." + extension);
            if (file.exists()) {
                return file;
            }
        }
        return null;
    }

    public static File tryGetFile(File folder, String... filenames) {
        for (String filename : filenames) {
            File file = new File(folder, filename);
            if (file.exists()) {
                return file;
            }
        }
        return null;
    }

    public static void main(String[] args) {

    }

}
